<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns:yui="http://yuilibrary.com/rdf/1.0/yui.rdf#">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	<title>API: highlight   highlight-base.js  (YUI Library)</title>

	<link rel="stylesheet" type="text/css" href="assets/reset-fonts-grids-min.css" />
	<link rel="stylesheet" type="text/css" href="assets/api.css" />

    <script type="text/javascript" src="assets/api-js"></script>
    <script type="text/javascript" src="assets/ac-js"></script>
</head>

<body id="yahoo-com">

<div id="doc3" class="yui-t2">
	<div id="hd">
        <h1><a href="http://developer.yahoo.com/yui/" title="Yahoo! UI Library">Yahoo! UI Library</a></h1>
        <h3>highlight&nbsp; <span class="subtitle">3.3.0</span></h3>
        <a href="./index.html" title="Yahoo! UI Library">Yahoo! UI Library</a> 
            &gt; <a href="./module_highlight.html" title="highlight">highlight</a>
                
                 &gt; highlight-base.js (source view) 
        <form onsubmit="return false">
            <div id="propertysearch">
                Search: <input autocomplete="off" id="searchinput" />
                <div id="searchresults">
                    &nbsp;
                </div>
            </div>
        </form>
	</div>

	<div id="bd">
		<div id="yui-main">
			<div class="yui-b">
            <form action="#" name="yui-classopts-form" method="get" id="yui-classopts-form">
                <fieldset>
                    <legend>Filters</legend>
                <span class="classopts"><input type="checkbox" name="show_private" id="show_private" /> <label for="show_private">Show Private</label></span>
                <span class="classopts"><input type="checkbox" name="show_protected" id="show_protected" /> <label for="show_protected">Show Protected</label></span>
                <span class="classopts"><input type="checkbox" name="show_deprecated" id="show_deprecated" /> <label for="show_deprecated">Show Deprecated</label></span>
                </fieldset>
            </form>

                    <div id="srcout">
                        <style>
                            #doc3 .classopts { display:none; }
                        </style>
                        <div class="highlight"><pre><span class="cm">/**</span>
<span class="cm"> * Provides methods for highlighting strings within other strings by wrapping</span>
<span class="cm"> * them in HTML.</span>
<span class="cm"> *</span>
<span class="cm"> * @module highlight</span>
<span class="cm"> * @since 3.3.0</span>
<span class="cm"> */</span>

<span class="cm">/**</span>
<span class="cm"> * &lt;p&gt;</span>
<span class="cm"> * Provides methods for highlighting strings within other strings by wrapping</span>
<span class="cm"> * them in HTML.</span>
<span class="cm"> * &lt;/p&gt;</span>
<span class="cm"> *</span>
<span class="cm"> * &lt;p&gt;</span>
<span class="cm"> * The highlight methods first escape any special HTML characters in the input</span>
<span class="cm"> * strings and then highlight the appropriate substrings by wrapping them in a</span>
<span class="cm"> * &lt;code&gt;&amp;lt;b class=&quot;yui3-highlight&quot;&amp;gt;&amp;lt;/b&amp;gt;&lt;/code&gt; element. The</span>
<span class="cm"> * &lt;code&gt;&amp;lt;b&amp;gt;&lt;/code&gt; element is used rather than</span>
<span class="cm"> * &lt;code&gt;&amp;lt;strong&amp;gt;&lt;/code&gt; in accordance with HTML5&#39;s definition of</span>
<span class="cm"> * &lt;code&gt;&amp;lt;b&amp;gt;&lt;/code&gt; as being purely presentational, which is exactly what</span>
<span class="cm"> * highlighting is.</span>
<span class="cm"> * &lt;/p&gt;</span>
<span class="cm"> *</span>
<span class="cm"> * @module highlight</span>
<span class="cm"> * @submodule highlight-base</span>
<span class="cm"> * @class Highlight</span>
<span class="cm"> * @static</span>
<span class="cm"> */</span>

<span class="kd">var</span> <span class="nx">YArray</span>    <span class="o">=</span> <span class="nx">Y</span><span class="p">.</span><span class="nb">Array</span><span class="p">,</span>
    <span class="nx">Escape</span>    <span class="o">=</span> <span class="nx">Y</span><span class="p">.</span><span class="nx">Escape</span><span class="p">,</span>
    <span class="nx">WordBreak</span> <span class="o">=</span> <span class="nx">Y</span><span class="p">.</span><span class="nx">Text</span><span class="p">.</span><span class="nx">WordBreak</span><span class="p">,</span>

    <span class="nx">isArray</span> <span class="o">=</span> <span class="nx">Y</span><span class="p">.</span><span class="nx">Lang</span><span class="p">.</span><span class="nx">isArray</span><span class="p">,</span>

    <span class="nx">EMPTY_OBJECT</span> <span class="o">=</span> <span class="p">{},</span>

    <span class="c1">// Regex string that captures zero or one unclosed HTML entities. Used in</span>
    <span class="c1">// the static regex template properties below. The entity matching is</span>
    <span class="c1">// intentionally loose here, since there&#39;s a world of complexity involved in</span>
    <span class="c1">// doing strict matching for this use case.</span>
    <span class="nx">UNCLOSED_ENTITY</span> <span class="o">=</span> <span class="s1">&#39;(&amp;[^;\\s]*)?&#39;</span><span class="p">,</span>

<span class="nx">Highlight</span> <span class="o">=</span> <span class="p">{</span>
    <span class="c1">// -- Protected Static Properties ------------------------------------------</span>

    <span class="cm">/**</span>
<span class="cm">     * &lt;p&gt;</span>
<span class="cm">     * Regular expression template for highlighting a match that occurs anywhere</span>
<span class="cm">     * in a string. The placeholder &lt;code&gt;%needles&lt;/code&gt; will be replaced with</span>
<span class="cm">     * a list of needles to match, joined by &lt;code&gt;|&lt;/code&gt; characters.</span>
<span class="cm">     * &lt;/p&gt;</span>
<span class="cm">     *</span>
<span class="cm">     * &lt;p&gt;</span>
<span class="cm">     * This regex should have two capturing subpatterns: the first should match</span>
<span class="cm">     * an unclosed HTML entity (e.g. &quot;&amp;amp&quot; without a &quot;;&quot; at the end) 0 or 1</span>
<span class="cm">     * times; the second should contain the &lt;code&gt;%needles&lt;/code&gt; placeholder.</span>
<span class="cm">     * The first subpattern match is used to emulate a negative lookbehind</span>
<span class="cm">     * assertion, in order to prevent highlighting inside HTML entities.</span>
<span class="cm">     * &lt;/p&gt;</span>
<span class="cm">     *</span>
<span class="cm">     * @property _REGEX</span>
<span class="cm">     * @type {String}</span>
<span class="cm">     * @protected</span>
<span class="cm">     * @static</span>
<span class="cm">     * @final</span>
<span class="cm">     */</span>
    <span class="nx">_REGEX</span><span class="o">:</span> <span class="nx">UNCLOSED_ENTITY</span> <span class="o">+</span> <span class="s1">&#39;(%needles)&#39;</span><span class="p">,</span>

    <span class="cm">/**</span>
<span class="cm">     * Regex replacer function or string for normal matches.</span>
<span class="cm">     *</span>
<span class="cm">     * @property _REPLACER</span>
<span class="cm">     * @type {Function|String}</span>
<span class="cm">     * @protected</span>
<span class="cm">     * @static</span>
<span class="cm">     * @final</span>
<span class="cm">     */</span>
    <span class="nx">_REPLACER</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">match</span><span class="p">,</span> <span class="nx">p1</span><span class="p">,</span> <span class="nx">p2</span><span class="p">)</span> <span class="p">{</span>
         <span class="c1">// Mimicking a negative lookbehind assertion to prevent matches inside</span>
         <span class="c1">// HTML entities. Hat tip to Steven Levithan for the technique:</span>
         <span class="c1">// http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript</span>
         <span class="k">return</span> <span class="nx">p1</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="p">(</span><span class="sr">/\s/</span><span class="p">).</span><span class="nx">test</span><span class="p">(</span><span class="nx">p2</span><span class="p">)</span> <span class="o">?</span> <span class="nx">match</span> <span class="o">:</span>
                    <span class="nx">Highlight</span><span class="p">.</span><span class="nx">_TEMPLATE</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\{s\}/g</span><span class="p">,</span> <span class="nx">p2</span><span class="p">);</span>
     <span class="p">},</span>

    <span class="cm">/**</span>
<span class="cm">     * &lt;p&gt;</span>
<span class="cm">     * Regular expression template for highlighting start-of-string matches</span>
<span class="cm">     * (i.e., only matches that occur at the beginning of a string). The</span>
<span class="cm">     * placeholder &lt;code&gt;%needles&lt;/code&gt; will be replaced with a list of needles</span>
<span class="cm">     * to match, joined by &lt;code&gt;|&lt;/code&gt; characters.</span>
<span class="cm">     * &lt;/p&gt;</span>
<span class="cm">     *</span>
<span class="cm">     * &lt;p&gt;</span>
<span class="cm">     * See &lt;code&gt;_REGEX&lt;/code&gt; for a description of the capturing subpatterns</span>
<span class="cm">     * this regex should contain.</span>
<span class="cm">     * &lt;/p&gt;</span>
<span class="cm">     *</span>
<span class="cm">     * @property _START_REGEX</span>
<span class="cm">     * @type {String}</span>
<span class="cm">     * @protected</span>
<span class="cm">     * @static</span>
<span class="cm">     * @final</span>
<span class="cm">     */</span>
    <span class="nx">_START_REGEX</span><span class="o">:</span> <span class="s1">&#39;^&#39;</span> <span class="o">+</span> <span class="nx">UNCLOSED_ENTITY</span> <span class="o">+</span> <span class="s1">&#39;(%needles)&#39;</span><span class="p">,</span>

    <span class="cm">/**</span>
<span class="cm">     * Highlight template which will be used as a replacement for matched</span>
<span class="cm">     * substrings. The placeholder &lt;code&gt;{s}&lt;/code&gt; will be replaced with the</span>
<span class="cm">     * matched substring.</span>
<span class="cm">     *</span>
<span class="cm">     * @property _TEMPLATE</span>
<span class="cm">     * @type {String}</span>
<span class="cm">     * @default &#39;&lt;b class=&quot;yui3-highlight&quot;&gt;{s}&lt;/b&gt;&#39;</span>
<span class="cm">     * @protected</span>
<span class="cm">     * @static</span>
<span class="cm">     * @final</span>
<span class="cm">     */</span>
    <span class="nx">_TEMPLATE</span><span class="o">:</span> <span class="s1">&#39;&lt;b class=&quot;yui3-highlight&quot;&gt;{s}&lt;/b&gt;&#39;</span><span class="p">,</span>

    <span class="c1">// -- Public Static Methods ------------------------------------------------</span>

    <span class="cm">/**</span>
<span class="cm">     * Highlights all occurrences in the &lt;em&gt;haystack&lt;/em&gt; string of the items</span>
<span class="cm">     * in the &lt;em&gt;needles&lt;/em&gt; array, regardless of where they occur. The</span>
<span class="cm">     * returned string will have all HTML characters escaped except for the</span>
<span class="cm">     * highlighting markup.</span>
<span class="cm">     *</span>
<span class="cm">     * @method all</span>
<span class="cm">     * @param {String} haystack String to apply highlighting to.</span>
<span class="cm">     * @param {String|Array} needles String or array of strings that should be</span>
<span class="cm">     *   highlighted.</span>
<span class="cm">     * @param {Object} options (optional) Options object, which may contain</span>
<span class="cm">     *   zero or more of the following properties:</span>
<span class="cm">     *</span>
<span class="cm">     * &lt;dl&gt;</span>
<span class="cm">     *   &lt;dt&gt;caseSensitive (Boolean)&lt;/dt&gt;</span>
<span class="cm">     *   &lt;dd&gt;</span>
<span class="cm">     *     If &lt;code&gt;true&lt;/code&gt;, matching will be case-sensitive. Default is</span>
<span class="cm">     *     &lt;code&gt;false&lt;/code&gt;.</span>
<span class="cm">     *   &lt;/dd&gt;</span>
<span class="cm">     *</span>
<span class="cm">     *   &lt;dt&gt;startsWith (Boolean)&lt;dt&gt;</span>
<span class="cm">     *   &lt;dd&gt;</span>
<span class="cm">     *     By default, needles are highlighted wherever they appear in the</span>
<span class="cm">     *     haystack. If &lt;code&gt;startsWith&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt;, matches</span>
<span class="cm">     *     must be anchored to the beginning of the string.</span>
<span class="cm">     *   &lt;/dd&gt;</span>
<span class="cm">     * &lt;/dl&gt;</span>
<span class="cm">     *</span>
<span class="cm">     * @return {String} Escaped and highlighted copy of &lt;em&gt;haystack&lt;/em&gt;.</span>
<span class="cm">     * @static</span>
<span class="cm">     */</span>
    <span class="nx">all</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">haystack</span><span class="p">,</span> <span class="nx">needles</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
        <span class="kd">var</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">len</span><span class="p">,</span> <span class="nx">regex</span><span class="p">,</span> <span class="nx">replacer</span><span class="p">;</span>

        <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
            <span class="nx">options</span> <span class="o">=</span> <span class="nx">EMPTY_OBJECT</span><span class="p">;</span>
        <span class="p">}</span>

        <span class="c1">// TODO: document options.replacer</span>
        <span class="nx">regex</span>    <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">startsWith</span> <span class="o">?</span> <span class="nx">Highlight</span><span class="p">.</span><span class="nx">_START_REGEX</span> <span class="o">:</span> <span class="nx">Highlight</span><span class="p">.</span><span class="nx">_REGEX</span><span class="p">;</span>
        <span class="nx">replacer</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">replacer</span> <span class="o">||</span> <span class="nx">Highlight</span><span class="p">.</span><span class="nx">_REPLACER</span><span class="p">;</span>

        <span class="c1">// Create a local copy of needles so we can safely modify it in the next</span>
        <span class="c1">// step.</span>
        <span class="nx">needles</span> <span class="o">=</span> <span class="nx">isArray</span><span class="p">(</span><span class="nx">needles</span><span class="p">)</span> <span class="o">?</span> <span class="nx">needles</span><span class="p">.</span><span class="nx">concat</span><span class="p">()</span> <span class="o">:</span> <span class="p">[</span><span class="nx">needles</span><span class="p">];</span>

        <span class="c1">// Escape HTML characters and special regular expression characters in</span>
        <span class="c1">// the needles so they can be used in a regex and matched against the</span>
        <span class="c1">// escaped haystack.</span>
        <span class="k">for</span> <span class="p">(</span><span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">len</span> <span class="o">=</span> <span class="nx">needles</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">len</span><span class="p">;</span> <span class="o">++</span><span class="nx">i</span><span class="p">)</span> <span class="p">{</span>
            <span class="nx">needles</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nx">Escape</span><span class="p">.</span><span class="nx">regex</span><span class="p">(</span><span class="nx">Escape</span><span class="p">.</span><span class="nx">html</span><span class="p">(</span><span class="nx">needles</span><span class="p">[</span><span class="nx">i</span><span class="p">]));</span>
        <span class="p">}</span>

        <span class="c1">// Escape HTML characters in the haystack to prevent HTML injection.</span>
        <span class="nx">haystack</span> <span class="o">=</span> <span class="nx">Escape</span><span class="p">.</span><span class="nx">html</span><span class="p">(</span><span class="nx">haystack</span><span class="p">);</span>

        <span class="k">return</span> <span class="nx">haystack</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span>
            <span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span>
                <span class="nx">regex</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="s1">&#39;%needles&#39;</span><span class="p">,</span> <span class="nx">needles</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39;|&#39;</span><span class="p">)),</span>
                <span class="nx">options</span><span class="p">.</span><span class="nx">caseSensitive</span> <span class="o">?</span> <span class="s1">&#39;g&#39;</span> <span class="o">:</span> <span class="s1">&#39;gi&#39;</span>
            <span class="p">),</span>
            <span class="nx">replacer</span>
        <span class="p">);</span>
    <span class="p">},</span>

    <span class="cm">/**</span>
<span class="cm">     * Same as &lt;code&gt;all()&lt;/code&gt;, but case-sensitive by default.</span>
<span class="cm">     *</span>
<span class="cm">     * @method allCase</span>
<span class="cm">     * @param {String} haystack String to apply highlighting to.</span>
<span class="cm">     * @param {String|Array} needles String or array of strings that should be</span>
<span class="cm">     *   highlighted.</span>
<span class="cm">     * @param {Object} options (optional) Options object. See &lt;code&gt;all()&lt;/code&gt;</span>
<span class="cm">     *   for details.</span>
<span class="cm">     * @return {String} Escaped and highlighted copy of &lt;em&gt;haystack&lt;/em&gt;.</span>
<span class="cm">     * @static</span>
<span class="cm">     */</span>
    <span class="nx">allCase</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">haystack</span><span class="p">,</span> <span class="nx">needles</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="nx">Highlight</span><span class="p">.</span><span class="nx">all</span><span class="p">(</span><span class="nx">haystack</span><span class="p">,</span> <span class="nx">needles</span><span class="p">,</span>
                <span class="nx">Y</span><span class="p">.</span><span class="nx">merge</span><span class="p">(</span><span class="nx">options</span> <span class="o">||</span> <span class="nx">EMPTY_OBJECT</span><span class="p">,</span> <span class="p">{</span><span class="nx">caseSensitive</span><span class="o">:</span> <span class="kc">true</span><span class="p">}));</span>
    <span class="p">},</span>

    <span class="cm">/**</span>
<span class="cm">     * Highlights &lt;em&gt;needles&lt;/em&gt; that occur at the start of &lt;em&gt;haystack&lt;/em&gt;.</span>
<span class="cm">     * The returned string will have all HTML characters escaped except for the</span>
<span class="cm">     * highlighting markup.</span>
<span class="cm">     *</span>
<span class="cm">     * @method start</span>
<span class="cm">     * @param {String} haystack String to apply highlighting to.</span>
<span class="cm">     * @param {String|Array} needles String or array of strings that should be</span>
<span class="cm">     *   highlighted.</span>
<span class="cm">     * @param {Object} options (optional) Options object, which may contain</span>
<span class="cm">     *   zero or more of the following properties:</span>
<span class="cm">     *</span>
<span class="cm">     * &lt;dl&gt;</span>
<span class="cm">     *   &lt;dt&gt;caseSensitive (Boolean)&lt;/dt&gt;</span>
<span class="cm">     *   &lt;dd&gt;</span>
<span class="cm">     *     If &lt;code&gt;true&lt;/code&gt;, matching will be case-sensitive. Default is</span>
<span class="cm">     *     &lt;code&gt;false&lt;/code&gt;.</span>
<span class="cm">     *   &lt;/dd&gt;</span>
<span class="cm">     * &lt;/dl&gt;</span>
<span class="cm">     *</span>
<span class="cm">     * @return {String} Escaped and highlighted copy of &lt;em&gt;haystack&lt;/em&gt;.</span>
<span class="cm">     * @static</span>
<span class="cm">     */</span>
    <span class="nx">start</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">haystack</span><span class="p">,</span> <span class="nx">needles</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="nx">Highlight</span><span class="p">.</span><span class="nx">all</span><span class="p">(</span><span class="nx">haystack</span><span class="p">,</span> <span class="nx">needles</span><span class="p">,</span>
                <span class="nx">Y</span><span class="p">.</span><span class="nx">merge</span><span class="p">(</span><span class="nx">options</span> <span class="o">||</span> <span class="nx">EMPTY_OBJECT</span><span class="p">,</span> <span class="p">{</span><span class="nx">startsWith</span><span class="o">:</span> <span class="kc">true</span><span class="p">}));</span>
    <span class="p">},</span>

    <span class="cm">/**</span>
<span class="cm">     * Same as &lt;code&gt;start()&lt;/code&gt;, but case-sensitive by default.</span>
<span class="cm">     *</span>
<span class="cm">     * @method startCase</span>
<span class="cm">     * @param {String} haystack String to apply highlighting to.</span>
<span class="cm">     * @param {String|Array} needles String or array of strings that should be</span>
<span class="cm">     *   highlighted.</span>
<span class="cm">     * @return {String} Escaped and highlighted copy of &lt;em&gt;haystack&lt;/em&gt;.</span>
<span class="cm">     * @static</span>
<span class="cm">     */</span>
    <span class="nx">startCase</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">haystack</span><span class="p">,</span> <span class="nx">needles</span><span class="p">)</span> <span class="p">{</span>
        <span class="c1">// No options passthru for now, since it would be redundant. If start()</span>
        <span class="c1">// ever supports more options than caseSensitive, then we&#39;ll start</span>
        <span class="c1">// passing the options through.</span>
        <span class="k">return</span> <span class="nx">Highlight</span><span class="p">.</span><span class="nx">start</span><span class="p">(</span><span class="nx">haystack</span><span class="p">,</span> <span class="nx">needles</span><span class="p">,</span> <span class="p">{</span><span class="nx">caseSensitive</span><span class="o">:</span> <span class="kc">true</span><span class="p">});</span>
    <span class="p">},</span>

    <span class="cm">/**</span>
<span class="cm">     * Highlights complete words in the &lt;em&gt;haystack&lt;/em&gt; string that are also</span>
<span class="cm">     * in the &lt;em&gt;needles&lt;/em&gt; array. The returned string will have all HTML</span>
<span class="cm">     * characters escaped except for the highlighting markup.</span>
<span class="cm">     *</span>
<span class="cm">     * @method words</span>
<span class="cm">     * @param {String} haystack String to apply highlighting to.</span>
<span class="cm">     * @param {String|Array} needles String or array of strings containing words</span>
<span class="cm">     *   that should be highlighted. If a string is passed, it will be split</span>
<span class="cm">     *   into words; if an array is passed, it is assumed to have already been</span>
<span class="cm">     *   split.</span>
<span class="cm">     * @param {Object} options (optional) Options object, which may contain</span>
<span class="cm">     *   zero or more of the following properties:</span>
<span class="cm">     *</span>
<span class="cm">     * &lt;dl&gt;</span>
<span class="cm">     *   &lt;dt&gt;caseSensitive (Boolean)&lt;/dt&gt;</span>
<span class="cm">     *   &lt;dd&gt;</span>
<span class="cm">     *     If &lt;code&gt;true&lt;/code&gt;, matching will be case-sensitive. Default is</span>
<span class="cm">     *     &lt;code&gt;false&lt;/code&gt;.</span>
<span class="cm">     *   &lt;/dd&gt;</span>
<span class="cm">     * &lt;/dl&gt;</span>
<span class="cm">     *</span>
<span class="cm">     * @return {String} Escaped and highlighted copy of &lt;em&gt;haystack&lt;/em&gt;.</span>
<span class="cm">     * @static</span>
<span class="cm">     */</span>
    <span class="nx">words</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">haystack</span><span class="p">,</span> <span class="nx">needles</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
        <span class="kd">var</span> <span class="nx">caseSensitive</span><span class="p">,</span>
            <span class="nx">mapper</span><span class="p">,</span>
            <span class="nx">template</span> <span class="o">=</span> <span class="nx">Highlight</span><span class="p">.</span><span class="nx">_TEMPLATE</span><span class="p">,</span>
            <span class="nx">words</span><span class="p">;</span>

        <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
            <span class="nx">options</span> <span class="o">=</span> <span class="nx">EMPTY_OBJECT</span><span class="p">;</span>
        <span class="p">}</span>

        <span class="nx">caseSensitive</span> <span class="o">=</span> <span class="o">!!</span><span class="nx">options</span><span class="p">.</span><span class="nx">caseSensitive</span><span class="p">;</span>

        <span class="c1">// Convert needles to a hash for faster lookups.</span>
        <span class="nx">needles</span> <span class="o">=</span> <span class="nx">YArray</span><span class="p">.</span><span class="nx">hash</span><span class="p">(</span>
            <span class="nx">isArray</span><span class="p">(</span><span class="nx">needles</span><span class="p">)</span> <span class="o">?</span> <span class="nx">needles</span> <span class="o">:</span> <span class="nx">WordBreak</span><span class="p">.</span><span class="nx">getUniqueWords</span><span class="p">(</span><span class="nx">needles</span><span class="p">,</span> <span class="p">{</span>
                <span class="nx">ignoreCase</span><span class="o">:</span> <span class="o">!</span><span class="nx">caseSensitive</span>
            <span class="p">})</span>
        <span class="p">);</span>

        <span class="c1">// The default word mapping function can be overridden with a custom</span>
        <span class="c1">// one. This is used to implement accent-folded highlighting in the</span>
        <span class="c1">// highlight-accentfold module.</span>
        <span class="nx">mapper</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">mapper</span> <span class="o">||</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">word</span><span class="p">,</span> <span class="nx">needles</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="nx">needles</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">(</span><span class="nx">caseSensitive</span> <span class="o">?</span> <span class="nx">word</span> <span class="o">:</span> <span class="nx">word</span><span class="p">.</span><span class="nx">toLowerCase</span><span class="p">()))</span> <span class="p">{</span>
                <span class="k">return</span> <span class="nx">template</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\{s\}/g</span><span class="p">,</span> <span class="nx">Escape</span><span class="p">.</span><span class="nx">html</span><span class="p">(</span><span class="nx">word</span><span class="p">));</span>
            <span class="p">}</span>

            <span class="k">return</span> <span class="nx">Escape</span><span class="p">.</span><span class="nx">html</span><span class="p">(</span><span class="nx">word</span><span class="p">);</span>
        <span class="p">};</span>

        <span class="c1">// Split the haystack into an array of words, including punctuation and</span>
        <span class="c1">// whitespace so we can rebuild the string later.</span>
        <span class="nx">words</span> <span class="o">=</span> <span class="nx">WordBreak</span><span class="p">.</span><span class="nx">getWords</span><span class="p">(</span><span class="nx">haystack</span><span class="p">,</span> <span class="p">{</span>
            <span class="nx">includePunctuation</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span>
            <span class="nx">includeWhitespace</span> <span class="o">:</span> <span class="kc">true</span>
        <span class="p">});</span>

        <span class="k">return</span> <span class="nx">YArray</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">words</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">word</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">return</span> <span class="nx">mapper</span><span class="p">(</span><span class="nx">word</span><span class="p">,</span> <span class="nx">needles</span><span class="p">);</span>
        <span class="p">}).</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">);</span>
    <span class="p">},</span>

    <span class="cm">/**</span>
<span class="cm">     * Same as &lt;code&gt;words()&lt;/code&gt;, but case-sensitive by default.</span>
<span class="cm">     *</span>
<span class="cm">     * @method wordsCase</span>
<span class="cm">     * @param {String} haystack String to apply highlighting to.</span>
<span class="cm">     * @param {String|Array} needles String or array of strings containing words</span>
<span class="cm">     *   that should be highlighted. If a string is passed, it will be split</span>
<span class="cm">     *   into words; if an array is passed, it is assumed to have already been</span>
<span class="cm">     *   split.</span>
<span class="cm">     * @return {String} Escaped and highlighted copy of &lt;em&gt;haystack&lt;/em&gt;.</span>
<span class="cm">     * @static</span>
<span class="cm">     */</span>
    <span class="nx">wordsCase</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">haystack</span><span class="p">,</span> <span class="nx">needles</span><span class="p">)</span> <span class="p">{</span>
        <span class="c1">// No options passthru for now, since it would be redundant. If words()</span>
        <span class="c1">// ever supports more options than caseSensitive, then we&#39;ll start</span>
        <span class="c1">// passing the options through.</span>
        <span class="k">return</span> <span class="nx">Highlight</span><span class="p">.</span><span class="nx">words</span><span class="p">(</span><span class="nx">haystack</span><span class="p">,</span> <span class="nx">needles</span><span class="p">,</span> <span class="p">{</span><span class="nx">caseSensitive</span><span class="o">:</span> <span class="kc">true</span><span class="p">});</span>
    <span class="p">}</span>
<span class="p">};</span>

<span class="nx">Y</span><span class="p">.</span><span class="nx">Highlight</span> <span class="o">=</span> <span class="nx">Highlight</span><span class="p">;</span>
</pre></div>

                    </div>
			</div>
		</div>
		<div class="yui-b">
            <div class="nav">

                    <div id="moduleList" class="module">
                        <h4>Modules</h4>
                        <ul class="content">
                                <li class=""><a href="module_align-plugin.html" title="align-plugin">align-plugin</a></li>
                                <li class=""><a href="module_anim.html" title="anim">anim</a></li>
                                <li class=""><a href="module_arraysort.html" title="arraysort">arraysort</a></li>
                                <li class=""><a href="module_async-queue.html" title="async-queue">async-queue</a></li>
                                <li class=""><a href="module_attribute.html" title="attribute">attribute</a></li>
                                <li class=""><a href="module_autocomplete.html" title="autocomplete">autocomplete</a></li>
                                <li class=""><a href="module_base.html" title="base">base</a></li>
                                <li class=""><a href="module_cache.html" title="cache">cache</a></li>
                                <li class=""><a href="module_charts.html" title="charts">charts</a></li>
                                <li class=""><a href="module_classnamemanager.html" title="classnamemanager">classnamemanager</a></li>
                                <li class=""><a href="module_collection.html" title="collection">collection</a></li>
                                <li class=""><a href="module_console.html" title="console">console</a></li>
                                <li class=""><a href="module_console-filters.html" title="console-filters">console-filters</a></li>
                                <li class=""><a href="module_cookie.html" title="cookie">cookie</a></li>
                                <li class=""><a href="module_dataschema.html" title="dataschema">dataschema</a></li>
                                <li class=""><a href="module_datasource.html" title="datasource">datasource</a></li>
                                <li class=""><a href="module_datatable.html" title="datatable">datatable</a></li>
                                <li class=""><a href="module_datatype.html" title="datatype">datatype</a></li>
                                <li class=""><a href="module_dd.html" title="dd">dd</a></li>
                                <li class=""><a href="module_dial.html" title="dial">dial</a></li>
                                <li class=""><a href="module_dom.html" title="dom">dom</a></li>
                                <li class=""><a href="module_dump.html" title="dump">dump</a></li>
                                <li class=""><a href="module_editor.html" title="editor">editor</a></li>
                                <li class=""><a href="module_escape.html" title="escape">escape</a></li>
                                <li class=""><a href="module_event.html" title="event">event</a></li>
                                <li class=""><a href="module_event-custom.html" title="event-custom">event-custom</a></li>
                                <li class=""><a href="module_event-gestures.html" title="event-gestures">event-gestures</a></li>
                                <li class=""><a href="module_event-simulate.html" title="event-simulate">event-simulate</a></li>
                                <li class=""><a href="module_event-touch.html" title="event-touch">event-touch</a></li>
                                <li class=""><a href="module_event-valuechange.html" title="event-valuechange">event-valuechange</a></li>
                                <li class="selected"><a href="module_highlight.html" title="highlight">highlight</a></li>
                                <li class=""><a href="module_history.html" title="history">history</a></li>
                                <li class=""><a href="module_history-deprecated.html" title="history-deprecated">history-deprecated</a></li>
                                <li class=""><a href="module_imageloader.html" title="imageloader">imageloader</a></li>
                                <li class=""><a href="module_intl.html" title="intl">intl</a></li>
                                <li class=""><a href="module_io.html" title="io">io</a></li>
                                <li class=""><a href="module_json.html" title="json">json</a></li>
                                <li class=""><a href="module_jsonp.html" title="jsonp">jsonp</a></li>
                                <li class=""><a href="module_loader.html" title="loader">loader</a></li>
                                <li class=""><a href="module_node.html" title="node">node</a></li>
                                <li class=""><a href="module_node-flick.html" title="node-flick">node-flick</a></li>
                                <li class=""><a href="module_node-focusmanager.html" title="node-focusmanager">node-focusmanager</a></li>
                                <li class=""><a href="module_node-menunav.html" title="node-menunav">node-menunav</a></li>
                                <li class=""><a href="module_oop.html" title="oop">oop</a></li>
                                <li class=""><a href="module_overlay.html" title="overlay">overlay</a></li>
                                <li class=""><a href="module_plugin.html" title="plugin">plugin</a></li>
                                <li class=""><a href="module_pluginhost.html" title="pluginhost">pluginhost</a></li>
                                <li class=""><a href="module_pluginhost-base.html" title="pluginhost-base">pluginhost-base</a></li>
                                <li class=""><a href="module_profiler.html" title="profiler">profiler</a></li>
                                <li class=""><a href="module_querystring.html" title="querystring">querystring</a></li>
                                <li class=""><a href="module_queue-promote.html" title="queue-promote">queue-promote</a></li>
                                <li class=""><a href="module_recordset.html" title="recordset">recordset</a></li>
                                <li class=""><a href="module_resize.html" title="resize">resize</a></li>
                                <li class=""><a href="module_scrollview.html" title="scrollview">scrollview</a></li>
                                <li class=""><a href="module_scrollview-base.html" title="scrollview-base">scrollview-base</a></li>
                                <li class=""><a href="module_scrollview-base-ie.html" title="scrollview-base-ie">scrollview-base-ie</a></li>
                                <li class=""><a href="module_scrollview-paginator.html" title="scrollview-paginator">scrollview-paginator</a></li>
                                <li class=""><a href="module_scrollview-scrollbars.html" title="scrollview-scrollbars">scrollview-scrollbars</a></li>
                                <li class=""><a href="module_shim-plugin.html" title="shim-plugin">shim-plugin</a></li>
                                <li class=""><a href="module_slider.html" title="slider">slider</a></li>
                                <li class=""><a href="module_sortable.html" title="sortable">sortable</a></li>
                                <li class=""><a href="module_stylesheet.html" title="stylesheet">stylesheet</a></li>
                                <li class=""><a href="module_substitute.html" title="substitute">substitute</a></li>
                                <li class=""><a href="module_swf.html" title="swf">swf</a></li>
                                <li class=""><a href="module_swfdetect.html" title="swfdetect">swfdetect</a></li>
                                <li class=""><a href="module_tabview.html" title="tabview">tabview</a></li>
                                <li class=""><a href="module_test.html" title="test">test</a></li>
                                <li class=""><a href="module_text.html" title="text">text</a></li>
                                <li class=""><a href="module_transition.html" title="transition">transition</a></li>
                                <li class=""><a href="module_uploader.html" title="uploader">uploader</a></li>
                                <li class=""><a href="module_widget.html" title="widget">widget</a></li>
                                <li class=""><a href="module_widget-anim.html" title="widget-anim">widget-anim</a></li>
                                <li class=""><a href="module_widget-base-ie.html" title="widget-base-ie">widget-base-ie</a></li>
                                <li class=""><a href="module_widget-child.html" title="widget-child">widget-child</a></li>
                                <li class=""><a href="module_widget-locale.html" title="widget-locale">widget-locale</a></li>
                                <li class=""><a href="module_widget-parent.html" title="widget-parent">widget-parent</a></li>
                                <li class=""><a href="module_widget-position.html" title="widget-position">widget-position</a></li>
                                <li class=""><a href="module_widget-position-align.html" title="widget-position-align">widget-position-align</a></li>
                                <li class=""><a href="module_widget-position-constrain.html" title="widget-position-constrain">widget-position-constrain</a></li>
                                <li class=""><a href="module_widget-stack.html" title="widget-stack">widget-stack</a></li>
                                <li class=""><a href="module_widget-stdmod.html" title="widget-stdmod">widget-stdmod</a></li>
                                <li class=""><a href="module_yql.html" title="yql">yql</a></li>
                                <li class=""><a href="module_yui.html" title="yui">yui</a></li>
                        </ul>
                    </div>

                    <div id="classList" class="module">
                        <h4>Classes</h4>
                        <ul class="content">
                                <li class=""><a href="Highlight.html" title="Highlight">Highlight</a></li>
                        </ul>
                    </div>

                    <div id="fileList" class="module">
                        <h4>Files</h4>
                        <ul class="content">        
                                <li class=""><a href="highlight-accentfold.js.html" title="highlight-accentfold.js">highlight-accentfold.js</a></li>
                                <li class="selected"><a href="highlight-base.js.html" title="highlight-base.js">highlight-base.js</a></li>
                        </ul>
                    </div>





            </div>
		</div>
	</div>
	<div id="ft">
        <hr />
        Copyright &copy; 2011 Yahoo! Inc. All rights reserved.
	</div>
</div>
<script type="text/javascript">

    var ALL_YUI_PROPS = [{"access": "", "host": "Highlight", "name": "all", "url": "Highlight.html#method_all", "type": "method"}, {"access": "", "host": "Highlight", "name": "allCase", "url": "Highlight.html#method_allCase", "type": "method"}, {"access": "", "host": "Highlight", "name": "allFold", "url": "Highlight.html#method_allFold", "type": "method"}, {"access": "protected", "host": "Highlight", "name": "_REGEX", "url": "Highlight.html#property__REGEX", "type": "property"}, {"access": "protected", "host": "Highlight", "name": "_REPLACER", "url": "Highlight.html#property__REPLACER", "type": "property"}, {"access": "", "host": "Highlight", "name": "start", "url": "Highlight.html#method_start", "type": "method"}, {"access": "", "host": "Highlight", "name": "startCase", "url": "Highlight.html#method_startCase", "type": "method"}, {"access": "", "host": "Highlight", "name": "startFold", "url": "Highlight.html#method_startFold", "type": "method"}, {"access": "protected", "host": "Highlight", "name": "_START_REGEX", "url": "Highlight.html#property__START_REGEX", "type": "property"}, {"access": "protected", "host": "Highlight", "name": "_TEMPLATE", "url": "Highlight.html#property__TEMPLATE", "type": "property"}, {"access": "", "host": "Highlight", "name": "words", "url": "Highlight.html#method_words", "type": "method"}, {"access": "", "host": "Highlight", "name": "wordsCase", "url": "Highlight.html#method_wordsCase", "type": "method"}, {"access": "", "host": "Highlight", "name": "wordsFold", "url": "Highlight.html#method_wordsFold", "type": "method"}];
</script>
</body>
</html>
